//
//  DetailViewController.swift
//  SwiftDemo
//
//  Created by Jason Jiang on 15/5/1.
//  Copyright (c) 2015年 Jason Jiang. All rights reserved.
//

import UIKit


class DetailViewController: UIViewController ,UICollectionViewDelegate , UICollectionViewDataSource{

    override func loadView() {
        
        super.loadView()
        self.view.backgroundColor =  UIColor.whiteColor()
        
        let layout :UICollectionViewFlowLayout = UICollectionViewFlowLayout ()
        layout.itemSize = CGSizeMake((CGRectGetWidth(self.view.frame) - 10)/2, 60);
        layout.scrollDirection = UICollectionViewScrollDirection.Vertical
        
        let clV:UICollectionView = UICollectionView (frame: CGRectZero, collectionViewLayout: layout)
        clV.delegate = self
        clV.dataSource = self
        clV.backgroundColor = UIColor.whiteColor()
        clV.frame = CGRectMake (0,10,CGRectGetWidth(self.view.frame),CGRectGetHeight(self.view.frame))
        clV.collectionViewLayout = layout
        clV.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        
        self.view.addSubview(clV)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //修改Nav 左右按钮
        
        let navBackItem = UIBarButtonItem (title: "返回", style: UIBarButtonItemStyle.Done, target: self, action: Selector ("backAction"))
        
        let attriDic :Dictionary = [NSForegroundColorAttributeName:UIColor.blackColor()]
        
        navBackItem.setTitleTextAttributes(attriDic, forState: UIControlState.Normal)
        self.navigationItem.leftBarButtonItem = navBackItem
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func backAction(){

        self.navigationController?.popToRootViewControllerAnimated(true)
    }
    
    
    
    
    //**
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 60
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell :UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) 
        
        cell.contentView.backgroundColor = UIColor.grayColor()
        
        var l  =  cell.contentView.viewWithTag(111111) as? UILabel
        if (l != nil) {
            l!.text = "label"
            
        }else{
        
         l = UILabel(frame: CGRectMake(0, 0, 60, 40))
         l!.text = "label"
         l!.tag = 111111
         cell.contentView.addSubview(l!)
        }
        return cell
    }
    
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        
        /**
        调转到1ViewCongtroller
           **/
        if(indexPath.item == 0){
            
           let delegateVC = FirstViewController();
           self.navigationController?.pushViewController(delegateVC, animated: true);
            
        }else if(indexPath.item == 1){
            
            let gasVC = GetAndSetViewController ();
            gasVC.str = "jhy"
            self.navigationController?.pushViewController(gasVC, animated: true)
        }else if(indexPath.item == 2 ){
            
            
            let imageVC = ImagePickerViewController (); 
            self.navigationController?.pushViewController(imageVC, animated: true);
            
        }else if(indexPath.item == 3){
            
            
            let blockVC = ReciveBlockViewController ();
            self.navigationController?.pushViewController(blockVC, animated: true);
            
        }else if(indexPath.item == 4){
            
            
            let blockVC = SortViewController ();
            self.navigationController?.pushViewController(blockVC, animated: true);
            
        }else if(indexPath.item == 5){
            
            
            let activityVC = ActivityViewController ();
            
            self.navigationController?.pushViewController(activityVC, animated: true);
        }
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
